package com.freshcity.manager.support
{
	import com.freshcity.core.MainScene;
	import com.freshcity.lib.util.ObjectPool;
	import com.freshcity.manager.MsgManager;
	import com.greensock.TimelineMax;
	import com.greensock.TweenLite;
	import com.pblabs.debug.Logger;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.StyleSheet;

	public class MsgPopupImpl extends Sprite
	{

		private var position:int;
		private var _currentMsg:*;
		private var res:ToolTipBg;
		private var _msgs:Vector.<Object>;
		private var _timeLine:TimelineMax;
		private var isFinish:Boolean = true;
		private var isStart:Boolean = false;

		public function MsgPopupImpl()
		{
			_msgs=new Vector.<Object>();
			var style1:StyleSheet=new StyleSheet();
			style1.setStyle("a:link", {fontFamily:'宋体', color: '#0000FF', fontSize: '12'});
			style1.setStyle("a:hover", {fontFamily:'宋体', color: '#ff0000', textDecoration: 'underline', fontSize: '12'});
			
			res=new ToolTipBg();
			res.words.styleSheet = style1;
			res.status.gotoAndStop(1);
			_timeLine=new TimelineMax({repeat: -1});
			_timeLine.addCallback(onTick, 0.2);
		}

		public function onTick():void
		{
			//Logger.debug(this, "onTick", String(this._msgs.length));
			if(!this.isFinish)return;
			
			if (this._msgs.length > 0)
			{
				_currentMsg=this._msgs.shift();
				this.isFinish=false;
				this.updateData();
				this.showTips();
			}
			else if (this._msgs.length <= 0)
			{
				_timeLine.stop();
				this.isStart=false;
				if (res.parent)removeChild(res);
				res.closeBtn.removeEventListener(MouseEvent.CLICK, hide);
			}
		}

		private function updateData():void
		{
			res.words.htmlText = _currentMsg.content;
			res.status.gotoAndStop(_currentMsg.status);
			if(_currentMsg.callback != null){
				res.words.addEventListener(TextEvent.LINK, onCallbackFun);
			}
			this.position=_currentMsg.position;
		}
		
		private function onCallbackFun(evt:TextEvent):void
		{
			_currentMsg.callback.call(null,null);
			this.hide();
		}

		private function showTips():void
		{
			res.alpha = 1;
			//LayerManager.alertLayer.addChild(arg1);
			TweenLite.from(res, 0.3, {onInit: resetPos, onInitParams: [res], onComplete: onCompleted, onCompleteParams: [res], y: "+200", alpha: 0});
		}

		public function showMsg(text:String, status:int, timeout:int, position:int,callback:Function):void
		{
			_msgs.push({content: text, status: status, timeout: timeout, position: position,callback:callback});
			if (!isStart)
			{
				_timeLine.play();
				isStart=true;
				res.alpha = 0;
				if (!res.parent)addChild(res);
				res.closeBtn.addEventListener(MouseEvent.CLICK, hide);				
			}
		}


		protected function onCompleted(res:ToolTipBg):void
		{
			if (this._currentMsg.timeout > 0)
			{
				TweenLite.delayedCall(_currentMsg.timeout, hide);
			}
		}

		private function hide(evt:Event=null):void
		{
			TweenLite.to(res, 0.3, {y: "+200", alpha: 0});
			if(this._currentMsg.callback != null){
				res.words.removeEventListener(TextEvent.LINK,onCallbackFun);
			}
			this.isFinish=true;
		}

		private function resetPos(obj:DisplayObject):void
		{
			switch (this.position)
			{
				case MsgManager.BOTTOM_RIGHT:
				{
					obj.x=stage.stageWidth - obj.width - 2;
					obj.y=stage.stageHeight - obj.height - 5;
					break;
				}

				default:
				{
					obj.x=(stage.stageWidth - obj.width) / 2;
					obj.y=(stage.stageHeight - obj.height) / 2;
					break;
				}
			}
		}
	}
}
